package com.krtech.wecard.module.sys.service.impl;

import cn.dev33.satoken.SaManager;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.model.CommRes;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.sys.entity.DeptOperateLog;
import com.krtech.wecard.module.sys.entity.SysDept;
import com.krtech.wecard.module.sys.entity.SysSchoolInfo;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.mapper.SysDeptMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.krtech.wecard.module.sys.service.DeptOperateLogService;
import com.krtech.wecard.module.sys.service.SysDeptService;
import com.krtech.wecard.module.sys.service.SysSchoolInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author laker
 * @since 2021-08-11
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private DeptOperateLogService deptOperateLogService;

    @Autowired
    private SysSchoolInfoService schoolInfoService;

    @Override
    public int updateBatch(List<SysDept> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<SysDept> list) {
        return baseMapper.batchInsert(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysDept mySaveOrUpdate(SysDept param) {

        SysDept dept = this.getOne(new QueryWrapper<SysDept>().eq(SysDept.COL_CODE,param.getCode()));

        if (StrUtil.isEmpty(param.getCode())){
            throw new BusinessException("必填参数code不能为空");
        }

        if (dept != null){
            param.setDeptId(dept.getDeptId());


            SysUser userInfo = LoginUtil.getUserInfo();
            //如果有id，不为空，就是更新，更新就要添加到日志


            //根据pcode查询上级
//            if (StrUtil.isNotEmpty(param.getPcode())){
//                SysDept parentDept = this.getOne(new QueryWrapper<SysDept>().eq(SysDept.COL_CODE,param.getPcode()));
//
//                if (parentDept != null){
//                    param.setPid(parentDept.getDeptId());
//                }else {
//                    param.setPid(0L);
//                }
//            }else {
//                param.setPid(0L);
//            }

//            param.setDeptId(dept.getDeptId());
            this.updateById(param);

            DeptOperateLog deptOperateLog = new DeptOperateLog();
            deptOperateLog.setCreatedBy(userInfo.getUserId().toString());
            deptOperateLog.setCreatedTime(new Date());
            deptOperateLog.setDeptName(dept.getDeptName());
            deptOperateLog.setDeptCode(dept.getCode());
            deptOperateLog.setFromInfo(JSONUtil.toJsonStr(dept));
            deptOperateLog.setToInfo(JSONUtil.toJsonStr(param));
            deptOperateLog.setType("UPDATE");
            deptOperateLogService.save(deptOperateLog);
        }else {

            param.setStatus(true);
            param.setSort(1);
            List<SysSchoolInfo> list = schoolInfoService.list();
            if (list.size() <= 0){
                throw new BusinessException("当前环境没有配置学校");
            }
            SysSchoolInfo schoolInfo = list.get(0);
            param.setFkSchoolCode(schoolInfo.getId().toString());

            //根据pcode查询上级
//            if (StrUtil.isNotEmpty(param.getPcode())){
//                SysDept parentDept = this.getOne(new QueryWrapper<SysDept>().eq(SysDept.COL_CODE,param.getPcode()));
//
//                param.setPid(parentDept.getDeptId());
//            }else {
//                param.setPid(0L);
//            }

            this.save(param);
        }

        return param;
    }

    @Override
    public List<SysDept> listDeptFromRedis() {
        String deptStr = SaManager.getSaTokenDao().get("listDeptFromRedis");
        if (StrUtil.isNotEmpty(deptStr)){
            List<SysDept> deptList = JSONUtil.toList(deptStr, SysDept.class);
            return deptList;
        }else {
            List<SysDept> deptList = this.list();
            String jsonStr = JSONUtil.toJsonStr(deptList);
            SaManager.getSaTokenDao().set("listDeptFromRedis",jsonStr,3600);
            return deptList;

        }
    }
}




